package cn.cloudreal.cbms.system.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.cloudreal.cbms.system.dao.PurviewDao;
import cn.cloudreal.cbms.vo.Purview;

/**
 * <p>
 * Title:CBMS
 * </p>
 * <p>
 * Description:权限查询数据Dao层实现类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2010
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author gaoxuejiao
 * @date 2012-07-16
 */
public class PurviewDaoImpl extends SqlMapClientDaoSupport implements
        PurviewDao
{

    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(PurviewDaoImpl.class);

    // 修改权限信息
    private static final String UPDATE_PUR = "purview.updatePurview";

    // 删除
    private static final String DEL_PUR = "purview.deletePurview";

    // 查询单个权限信息
    private static final String GET_PUR = "purview.getPurview";

    // 查询所有权限信息
    private static final String GET_PUR_All = "purview.getAllPurview";

    // 新增权限信息
    private static final String ADD_PUR = "purview.insertPurview";

    // 新增权限权限中间表信息
    private static final String ADD_PUR_PRIV = "purview.insertPurviewPriv";

    // 根据用户权限编号查询所有在使用的权限列表信息
    private static final String GET_PUR_BY_ROLE = "purview.getPurviewByRole";

    // 根据用户权限编号查询所有在使用的权限列表信息
    private static final String IS_USE_PUR = "purview.isUse";

    /**
     * 根据roleCode查询集合信息
     * 
     * @param page
     * @return Purview
     */
    public List<Purview> getPurviewsByRole(String roleCode)
    {
        List<Purview> PurviewList = null;

        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter PurviewleDaoImpl getPurviewsByRole() method,return Purview");
        }
        PurviewList = new ArrayList<Purview>();
        // 执行当个Purview 对象查询
        PurviewList = getSqlMapClientTemplate().queryForList(GET_PUR_BY_ROLE,
                roleCode);
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit PurviewleDaoImpl getPurviewsByRole() method,return Purview");
        }
        return PurviewList;
    }

    /**
     * 根据权限id 查询是否被使用
     * 
     * @param page
     * @return Purview
     */
    public boolean isUsePurview(Integer id)
    {

        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter PurviewleDaoImpl getPurviewsByRole() method,return Purview");
        }

        // Purview pr = getPurview(id);

        // if (null != pr && null != pr.getPrivCode())
        // {
        // 执行当个Purview 对象查询
        Integer count = (Integer) getSqlMapClientTemplate().queryForObject(IS_USE_PUR,
                id.toString());
        if (null != count && count.intValue() > 0)
        {
            return true;
        }
        // }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit PurviewleDaoImpl getPurviewsByRole() method,return Purview");
        }
        return false;
    }

    /**
     * 根据id查询单个数据
     * 
     * @param page
     * @return Purview
     */
    public Purview getPurview(Integer id)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter PurviewleDaoImpl getPurview() method,return Purview");
        }
        // 执行当个Purview 对象查询
        Purview purview = (Purview) getSqlMapClientTemplate().queryForObject(GET_PUR,
                id);

        /**
         * 如果存在父节点在获取
         */
        if (null != purview && null != purview.getParentId()
                && !purview.getParentId().equals(-1))
        {
            Purview temp = (Purview) getSqlMapClientTemplate().queryForObject(GET_PUR,
                    purview.getParentId());
            purview.setParentName(temp.getActionName());
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit PurviewleDaoImpl getPurview() method,return Purview");
        }
        return purview;
    }

    /**
     * 查询所有权限列表结合
     * 
     * @return PurviewList
     */
    public List<Purview> getAllPurviews(Purview purview)
    {
        List<Purview> PurviewList = null;

        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter PurviewleDaoImpl getAllPurviews() method,return PurviewList");
        }
        PurviewList = new ArrayList<Purview>();

        // 执行当个Purview 对象查询
        PurviewList = getSqlMapClientTemplate().queryForList(GET_PUR_All,
                purview);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit PurviewleDaoImpl getAllPurviews() method,return PurviewList");
        }

        return PurviewList;
    }

    /**
     * 更新数据
     * 
     * @param purview
     * @return
     */
    public void updatePurview(Purview purview)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter PurviewleDaoImpl updatePurview() method");
        }

        getSqlMapClientTemplate().update(UPDATE_PUR, purview);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit PurviewleDaoImpl updatePurview() method");
        }
    }

    /**
     * 删除数据
     * 
     * @param page
     * @return
     */
    public void delPurview(Purview purview)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter PurviewleDaoImpl delPurview() method");
        }
        if (null != purview && null != purview.getId())
        {
            getSqlMapClientTemplate().delete(DEL_PUR, purview.getId());
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit PurviewleDaoImpl delPurview() method");
        }
    }

    /**
     * 新增数据
     * 
     * @param page
     * @return
     */
    public int addPurview(Purview purview)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter PurviewleDaoImpl addPurview() method");
        }
        Integer id = (Integer) getSqlMapClientTemplate().insert(ADD_PUR,
                purview);
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit PurviewleDaoImpl addPurview() method");
        }
        if (null != id)
        {
            return id.intValue();
        }
        return 0;
    }
}
